package com.younion.dao;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.younion.common.base.BaseDAO;
import com.younion.common.base.BaseVO;
import com.younion.common.exception.BaseException;
import com.younion.common.paginator.Paginator;
import com.younion.vo.ActiveVO;

public class ActiveDAO extends BaseDAO {

	protected static final Log logger = LogFactory.getLog(ActiveDAO.class);

	public ActiveVO selectActiveById(int id) throws BaseException {
		try {
			return (ActiveVO) this.getSqlMapClientTemplate().queryForObject(
					"t_active.selectById", id);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	public boolean updateActiveByMap(Map paramMap) throws BaseException {
		try {
			this.update("t_active.updateByMap", paramMap);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					"ActiveDAO，调用updateActiveByMap(Map paramMap)方法出错");
		}
	}

	public boolean deleteActiveById(int id) throws BaseException {
		try {
			this.getSqlMapClientTemplate().delete("t_active.deleteById", id);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	public boolean deleteActiveByIds(List list) throws BaseException {
		try {
			this.getSqlMapClientTemplate().delete("t_active.deleteByIds", list);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					"ActiveDAO，调用deleteActiveByIds(List list)方法出错");
		}
	}

	/**
	 * 查询记录数
	 * 
	 * @param paramMap
	 * @return
	 * @throws BaseException
	 */
	public int selectCountByMap(Map paramMap) throws BaseException {
		if (paramMap == null)
			return 0;
		try {
			return queryCount("t_active.selectByMapCount", paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	/**
	 * 实现翻页
	 * 
	 * @param condition
	 *            condition{Vo:条件VO对象，curPage:当前页面数，pageSize:每页最大行数}
	 * @return type : Page 返回一个分页的List，操作失败返回null
	 */
	public List selectForPage(Map condition, Paginator paginator)
			throws BaseException {
		if (condition == null)
			return null;
		int pageIndex = paginator.getStartIndex();
		int perPageSize = paginator.getPageSize();
		String sqlStr = "";
		try {
			Map vo = (Map) condition.get("paramMap");
			if (vo == null) {
				sqlStr = "t_active.selectAll";
			} else {
				sqlStr = "t_active.selectByMap";
			}
			return queryForPage(sqlStr, vo, pageIndex, perPageSize);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					"ActiveDAO，调用selectForPage（Map condition, Paginator paginator）方法出错");
		}
	}

	/**
	 * 向数据库中插入一个VO对象
	 * 
	 * @param activeVO
	 *            对象对应到数据库中的一条记录
	 * @return type : ActiveVO 返回操作是否成功
	 */
	public Integer insertByVo(ActiveVO activeVO) throws BaseException {
		if (activeVO == null)
			return null;
		try {
			return (Integer) this.getSqlMapClientTemplate().insert(
					"t_active.insertByVo", (BaseVO) activeVO);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					"ActiveDAO，调用insertByVo(ActiveVO activeVO)方法出错");
		}
	}

	public void updateTopById(Map map) throws BaseException {
		try {
			this.getSqlMapClientTemplate()
					.update("t_active.updateTopById", map);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	public int selectMaxTop() throws BaseException {
		try {
			Object obj = this.getSqlMapClientTemplate().queryForObject(
					"t_active.selectMaxTop");
			if (obj == null)
				return 0;
			return ((Integer) obj).intValue();
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	public List<ActiveVO> selectByIds(List list) throws BaseException {
		try {
			return this.getSqlMapClientTemplate().queryForList(
					"t_active.selectByIds", list);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	public List<ActiveVO> selectActivitysByCardMap(Map map)
			throws BaseException {
		try {
			return this.getSqlMapClientTemplate().queryForList(
					"t_active.selectActivitysByCardMap", map);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	public Collection selectApiAll(Map map) throws BaseException {
		return queryForList("t_active.selectApiAll", map);
	}
	
	public Collection selectByMap(Map map) throws BaseException{
		
		try{
			return this.queryForList("t_active.selectByMap", map);
		}catch(Exception e){
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
		}
}
